1   package uba.db.column.io;
2   
3   import java.io.ByteArrayInputStream;
4   import java.io.ByteArrayOutputStream;
5   import java.io.DataInputStream;
6   import java.io.DataOutputStream;
7   
8   import junit.framework.TestCase;
9   import uba.db.column.CharColumnSpecification;
10  
11  /***
12   * @version $Revision: 1.3 $
13   */
14  public class CharColumnWriterTest extends TestCase {
15      private static final int MAX_CHARS = 10;
16      private CharColumnWriter writer;
17      private ByteArrayOutputStream stream;
18  
19      /***
20       * @see junit.framework.TestCase#setUp()
21       */
22      protected void setUp() throws Exception {
23          stream = new ByteArrayOutputStream();
24          writer = new CharColumnWriter(new CharColumnSpecification("prueba", MAX_CHARS),
25                  new DataOutputStream(stream));
26      }
27  
28      /***
29       * Test: escribir el string 'abc' a un stream de bytes.
30       */
31      public void testWrite() throws Exception {
32          writer.write("abc");
33  
34          byte[] result = stream.toByteArray();
35          assertEquals("cada caracter ocupa 2 bytes", MAX_CHARS * 2, result.length);
36  
37          DataInputStream resultStream = new DataInputStream(new ByteArrayInputStream(
38                  result));
39          assertEquals('a', resultStream.readChar());
40          assertEquals('b', resultStream.readChar());
41          assertEquals('c', resultStream.readChar());
42  
43          // el resto de los caracteres debe ser null
44          for (int i = 3; i < 10; i++) {
45              assertEquals('\0', resultStream.readChar());
46          }
47      }
48  }